package tt.com.carmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.carmanager.mapper.DriveTaskMapper;
import tt.com.carmanager.mapper.LoginRoleMapper;
import tt.com.carmanager.mapper.YxCommonDrivetaskGoodsMapper;
import tt.com.carmanager.mapper.YxCommonDrivetaskGoodsTotalCostMapper;
import tt.com.carmanager.service.DrivetaskGoodsService;
import tt.com.carmanager.service.FuneralDriverTaskService;
import tt.com.carmanager.vo.TeamTimeVo;
import tt.com.common.enums.Status;
import tt.com.common.model.*;
import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FuneralDriverTaskServiceImpl extends ServiceImpl<DriveTaskMapper, DriveTask> implements FuneralDriverTaskService {

    @Autowired
    private DriveTaskMapper driveTaskMapper;

    @Autowired
    private LoginRoleMapper loginRoleMapper;

    @Autowired
    private YxCommonDrivetaskGoodsMapper drivetaskGoodsMapper;  // 存放订单所需物品表

    @Autowired
    private YxCommonDrivetaskGoodsTotalCostMapper drivetaskGoodsTotalCostMapper;  // 存放总金额的表

    @Autowired
    private DrivetaskGoodsService  drivetaskGoodsService;

    @Override
    public List<DriveTask> showTaskByUserName(String username) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        LoginRole loginRole = loginRoleMapper.selectOne(wrapper);
        if (loginRole == null){
            return null;
        }

        String roleCodeByUserName = loginRoleMapper.getRoleCodeByUserName(username);

        if (roleCodeByUserName.equals("admin")){
            return driveTaskMapper.selectList(null);
        }
            return driveTaskMapper.showTaskForDriver(username);
    }

    @Override
    public PageUtil showTaskByPage(PageUtil pageUtil) {
        Page<DriveTask> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        LambdaQueryWrapper<DriveTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DriveTask::getDataStatus, 1);
        IPage<DriveTask> iPage = driveTaskMapper.selectPage(page, wrapper);
        pageUtil.setTotalSize(Math.toIntExact(iPage.getTotal()));
        pageUtil.setData(iPage);
        return pageUtil;
    }

    @Override
    public List<DriveTask> showTaskRefreshByUserName(String username) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        LoginRole loginRole = loginRoleMapper.selectOne(wrapper);
        if (loginRole == null){
            return null;
        }

        String roleCodeByUserName = loginRoleMapper.getRoleCodeByUserName(username);

        if (roleCodeByUserName.equals("admin")){
            return driveTaskMapper.selectList(null);
        }
        return driveTaskMapper.showTaskForDriver(username);
    }

    @Override
    public Boolean showTaskDeleteById(String taskId, String delUserId) {
        // 判断删除得的任务是否存在任务列表中
        DriveTask driveTask = driveTaskMapper.selectById(taskId);
        if (driveTask == null){
            return false;
        }

        // 设置删除用户的id
        driveTask.setDeleteUserId(delUserId);

        // 设置任务状态
        // 设置任务状态
        driveTask.setDataStatus(0);
        return driveTaskMapper.updateById(driveTask) > 0;
    }

    @Override
    public Boolean showTaskChooseStart(DriveTask driveTask) {
        return driveTaskMapper.updateById(driveTask) > 0;
    }

    @Override
    public Boolean showTaskChooseReturn(DriveTask driveTask) {
        return driveTaskMapper.updateById(driveTask) > 0;
    }

    @Override
    public CommonResult chooseDriveGoods(List<YxCommonDrivetaskGoods> drivetaskGoods, String taskId) {
        // 总金额
        BigDecimal totalCoast = new BigDecimal("0");

        for (YxCommonDrivetaskGoods drivetaskGood : drivetaskGoods) {
            if (!ObjectUtil.objectIsNull(drivetaskGood)){
                return CommonResult.buildR(Status.PARAM_ISNULL);
            }
            drivetaskGood.setTaskId(taskId);
            // 遍历出当前任务所需物品的所有单价后再累加起来算出  总价格
            totalCoast = totalCoast.add(drivetaskGood.getGoodsPrice());
        }

        // 存入yx_common_drivetask_goodsTotalCost表中
        if (!drivetaskGoodsService.saveBatch(drivetaskGoods)){
            return CommonResult.buildR(Status.SYSTEM_ERROR);
        }

           YxCommonDrivetaskGoodsTotalCost yxCommonDrivetaskGoodsTotalCost = new YxCommonDrivetaskGoodsTotalCost();
        // 设置当前任务所有物品的累加总金额
        yxCommonDrivetaskGoodsTotalCost.setTotalcost(totalCoast);
        // 设置对应的任务id
        yxCommonDrivetaskGoodsTotalCost.setTaskId(taskId);
        if (drivetaskGoodsTotalCostMapper.insert(yxCommonDrivetaskGoodsTotalCost) <= 0){
            return CommonResult.buildR(Status.SYSTEM_ERROR, "订单生成失败");
        }

        return CommonResult.buildR(Status.OK, "当前任务物品选择成功");
    }

    @Override
    public CommonResult countCostByTeamId(TeamTimeVo teamTimeVo) {
        // 根据时间段和班组id查询范围数
        List<DriveTask> driveTasksList = driveTaskMapper.searchIdByTeamIdAndDate(teamTimeVo);

        // 统计当前时间段内的总金额
        BigDecimal count = new BigDecimal("0");

        if (driveTasksList == null || driveTasksList.size() <= 0){
            return CommonResult.buildR(Status.PARAM_ISNULL, "该班组在指定日期内无数据");
        }

        for (DriveTask driveTask : driveTasksList) {
            // 查询当前任务所需物品的总价
            List<YxCommonDrivetaskGoodsTotalCost> allGoodsTotalCost = drivetaskGoodsTotalCostMapper.selectList(new QueryWrapper<YxCommonDrivetaskGoodsTotalCost>().eq("task_id", driveTask.getId()));
            for (YxCommonDrivetaskGoodsTotalCost yxCommonDrivetaskGoodsTotalCost : allGoodsTotalCost) {
                count = count.add(yxCommonDrivetaskGoodsTotalCost.getTotalcost());
            }
        }
        return CommonResult.buildR(Status.OK, count);
    }
}
